﻿/*
COPYRIGHT (C) 2011-2013 Raynald Messié

THIS FILE IS PART OF SCRUMPILOT.

SCRUMPILOT IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF 
THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

SCRUMPILOT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT
EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER
GENERAL PUBLIC LICENSE FOR MORE DETAILS.

YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH SCRUMPILOT. 
IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/

using System;
using System.Web.Mvc;
using Elmah;
using ScrumPilot.Domain.BPL;
using ScrumPilot.Domain.Models;
using ScrumPilot.Helpers;
using ScrumPilot.TFSHelpers;

namespace ScrumPilot.Controllers
{
    [Authorize]
    public class TasksController : BaseController
    {
        private TaskServices _tskm;
        protected TaskServices TskModel { get { return _tskm ?? (_tskm = new TaskServices(this.TFS)); } }

        [HttpGet, NoCache]
        public ActionResult TasksList()
        {
            var lst = (new ProjectServices(TFS)).GetProjectsFiltered(this.CurrentUserDB.FilterListByProject);
            return View(lst);
        }

        public PartialViewResult TasksListItem()
        {
            return PartialView();
        }

        [HttpGet, NoCache]
        public JsonResult GetTasks()
        {
            return Json(TskModel.GetTasks(CurrentUser,  CurrentCulture), JsonRequestBehavior.AllowGet);
        }

        [HttpGet, NoCache]
        public PartialViewResult TaskScore(Int32 id)
        {
            try
            {
                return PartialView(TskModel.GetTaskScore(id));
            }
            catch (Exception ex)
            {
                ErrorSignal.FromCurrentContext().Raise(ex);
                throw;
            }
        }

        [HttpPost]
        public EmptyResult TaskScore(TaskForScore model)
        {
            try
            {
                TskModel.SetTaskScore(model);
                return null;
            }
            catch (Exception ex)
            {
                ErrorSignal.FromCurrentContext().Raise(ex);
                throw;
            }
        }

        [HttpGet, NoCache]
        public PartialViewResult TaskMove(Int32 id)
        {
            try
            {
                return PartialView(TskModel.GetTaskMove(id));
            }
            catch (Exception ex)
            {
                ErrorSignal.FromCurrentContext().Raise(ex);
                throw;
            }
        }

        [HttpPost]
        public EmptyResult TaskMove(TaskForMove model)
        {
            try
            {
                TskModel.SetTaskMove(model, this.TFS);
                return null;
            }
            catch (Exception ex)
            {
                ErrorSignal.FromCurrentContext().Raise(ex);
                throw;
            }
        }

        [HttpGet]
        public JsonResult GetAreas(String sprintPath)
        {
            var areas = (new ProjectServices(TFS)).GetAreas(sprintPath);
            return Json(areas, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public EmptyResult SetFilterValue(string key, Boolean data)
        {
            TskModel.SetFilterTasksList(CurrentUser, key, data);
            return null;
        }

        [HttpPost]
        public EmptyResult FilterByProject(int[] myprojects)
        {
            TskModel.SetFilterTasksList(CurrentUser, myprojects);
            return null;
        }

        [HttpGet, NoCache]
        public JsonResult GetFilterValue()
        {
            return Json(TskModel.GetFilterCheckBoxList(CurrentUser), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public EmptyResult TaskAction(int id, byte typeAction)
        {
            try
            {
                switch (typeAction)
                {
                    case 0: // I take it
                        TskModel.UpdateAssignedTask(id, this.CurrentUserDB.name);
                        break;
                    case 1: // Done
                        TskModel.UpdateStateTask(id, WorkItemState.Done);
                        break;
                    case 2: // I give up
                        TskModel.UpdateAssignedTask(id, String.Empty);
                        break;
                }
            }
            catch (Exception ex)
            {
                ErrorSignal.FromCurrentContext().Raise(ex);
                throw;
            }

            return null;
        }

        [HttpPost]
        public ContentResult UpdateInlineTask(string id, string value)
        {
            Int32 wiID = Int32.Parse(id.Substring(id.IndexOf('_') + 1));
            String fieldType = id.Substring(0, id.IndexOf('_')).ToLower();

            String result = TskModel.UpdateInlineWorkItem(fieldType, wiID, value) ? "ok" : "bad";

            return Content(result);
        }
    }
}